// pages/shangpudetail/shangpudetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    store_id:0,//商铺id

    swiperCurrent: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 800,
    circular: true,
    
    sp_detail: {},
    recommend_food: '推荐商品',

    hideHeader: true,
    hideBottom: true,
    refreshTime: '', // 刷新的时间 
    allPages: '', // 总页数
    currentPage: 1, // 当前页数  默认是1
    loadMoreData: '加载更多……',

    isHiddenMes1: true,
    pinglun_num: 216,//评论数量
    averageScore: 4.9,//平均分数
    zan_num: 129,//点赞数量
    pinglun_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var date = new Date();
    this.setData({
      refreshTime: date.toLocaleTimeString()
    })
    
    var id = parseInt(options.id);
    this.setData({
      store_id:id
    })
    //获取商铺数据
    this.storeDetailData(id);

    // 获取评论列表数据
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
   /**
    * 下拉刷新
    */ 
  refresh:function(){
    var self = this;
    setTimeout(function () {
      console.log('下拉刷新');
      var date = new Date();
      self.setData({
        currentPage: 1,
        refreshTime: date.toLocaleTimeString(),
        hideHeader: false
      })
      self.getData();
    }, 300);
  },
  /**
   *上拉加载更多
   */ 
  loadMore:function(){
    var self = this;
    // 当前页是最后一页
    if (self.data.currentPage == self.data.allPages) {
      self.setData({
        loadMoreData: '已经到顶'
      })
      return;
    }
    setTimeout(function () {
      console.log('上拉加载更多');
      var tempCurrentPage = self.data.currentPage;
      tempCurrentPage = tempCurrentPage + 1;
      self.setData({
        currentPage: tempCurrentPage,
        hideBottom: false
      })
      self.getData();
    }, 300);
  },
  /**
   * 获取更多
   */
  moreOrder: function () {
    console.log('获取更多');
    var that = this;

    var obj = {};
    obj.id = that.data.store_id;
    obj.pagename = 'shangpudetail';
    obj = JSON.stringify(obj);

    wx.navigateTo({
      url: '../tuijianfood/tuijianfood?obj=' + obj,
    })
  },
  /*
  *商铺详细信息
  */
  storeDetailData:function(id){
    var that = this;
    app.apiPost(app.apiList.storeDetail, {
      content:{
        id:id
      }
    }, function (res) {
      console.log(res,'商铺详细信息');
      that.setData({
        sp_detail: res.content
      })
    })
  },
  /*
  *评论数据
  */
  getData: function () {
    var self = this;
    var pageIndex = self.data.currentPage;

    app.apiPost(app.apiList.meishi_comment_list, {
      
      content:{
        pageNum:pageIndex,
        pageSize:20,
        siteId: self.data.store_id
      }
    }, function (res) {
      console.log(res, '商铺评论');

      var pinglun_list = res.content.pageData.list;

      if (pageIndex == 1) { // 下拉刷新
        self.setData({
          allPages: res.content.pageData.total,
          pinglun_list: pinglun_list,
          hideHeader: true
        })

      } else { // 加载更多
        var tempArray = self.data.pinglun_list;
        tempArray = tempArray.concat(pinglun_list);
        self.setData({
          allPages: res.content.pageData.total,
          pinglun_list: tempArray,
          hideBottom: true
        })
      }

      if (self.data.pinglun_list.length > 0) {
        self.setData({
          isHiddenMes1: false
        })
      }
    })

  },
  fooddetail: function (e) {
    console.log('推荐菜的具体。。', e.currentTarget.dataset.index);
  },
  searchAllPL: function () {
    console.log('查看所有评论');
    var self = this;
    wx.navigateTo({
      url: '../allcomment/allcomment?siteId=' + self.data.store_id,
    })
  },
  yaoPinglun: function () {
    console.log('我要评论');
    wx.navigateTo({
      url: '../publishcomment/publishcomment',
    })
  },
  //轮播图的切换事件
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  //点击指示点切换
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  //点击图片触发事件
  swipclick: function (e) {
    console.log(this.data.swiperCurrent);
    wx.switchTab({
      url: this.data.links[this.data.swiperCurrent]
    })
  },
  searchDaohang:function(e){
    console.log('导航');

    var obj = {};
    obj.name = this.data.sp_detail.name;
    obj.lat = this.data.sp_detail.lat;
    obj.lng = this.data.sp_detail.lng;
    obj.address = this.data.sp_detail.address;

    obj = JSON.stringify(obj);

    wx.navigateTo({
      url: '../shopnavigate/shopnavigate?storeInfo=' + obj,
    })
  },
  cellPhone:function(){
    console.log('拨打电话');
    var phone = this.data.sp_detail.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})